#include "mainscene.h"
#include"config.h"
#include "map.h"
#include<QIcon>
#include<QPainter>
#include<QMouseEvent>
#include<ctime>

MainScene::MainScene(QWidget *parent)
    : QWidget(parent)
{
    //调用初始化场景
    initScene();
    //启动游戏
    playGame();
}

MainScene::~MainScene()
{
}

void MainScene::initScene()
{
    //设置窗口固定
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    //设置标题
    setWindowTitle(GAME_TITLE);
    //设置图标
    setWindowIcon(QIcon(":/resource/title"));
    //定时器
    m_Timer.setInterval(GAME_RATE);
    //敌机出场时间间隔 初始化
    m_recorder=0;
    m_recorder2=0;
    m_recorder3=0;
    //随机数种子
    srand((unsigned int)time(NULL));
}

void MainScene::playGame()
{
    //启动定时器
    m_Timer.start();

    //监听定时器信号
    connect(&m_Timer,&QTimer::timeout,[=](){
        //敌机出场
        enemy1ToScene();

        //敌机2出场
        enemy2ToScene();

        //敌机3出场
        enemy3ToScene();

        //更新游戏中元素坐标
        updatePosition();

        //绘制到屏幕中
        update();

        //碰撞检测
        collisionDetection();

        //碰撞2检测
        collisionDetection2();

        //碰撞3检测
        collisionDetection3();
    });
}

void MainScene::updatePosition()
{
    //更新地图坐标
    m_map.mapPosition();

    //发射子弹
    m_hero.shoot();

    //计算所有非空闲子弹当前坐标
    for(int i=0;i<BULLET_NUM;i++)
    {
        if(m_hero.m_bullets[i].m_Free==false)
        {
            m_hero.m_bullets[i].updatePosition();
        }
    }

    //敌机出场
    for(int i=0;i<ENEMY1_NUM;i++)
    {
        if(m_enemy1s[i].m_Free==false)
        {
            m_enemy1s[i].updatePosition();
        }
    }

    //敌机出场
    for(int i=0;i<ENEMY2_NUM;i++)
    {
        if(m_enemy2s[i].m_Free2==false)
        {
            m_enemy2s[i].updatePosition();
        }
    }

    //敌机出场
    for(int i=0;i<ENEMY3_NUM;i++)
    {
        if(m_enemy3s[i].m_Free3==false)
        {
            m_enemy3s[i].updatePosition();
        }
    }

    //计算爆炸播放图片
    for(int i=0;i<BOMB_NUM;i++)
    {
        if(m_bombs[i].m_Free==false)
        {
            m_bombs[i].updateInfo();
        }
    }

    //测试子弹
    //temp_Bullet.m_Free=false;
    //temp_Bullet.updatePosition();
}

void MainScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    //绘制地图
    painter.drawPixmap(0,m_map.m_map1_posY,m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY,m_map.m_map2);

    //绘制豌豆
    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);

    //绘制子弹(非空闲)
    for(int i=0;i<BULLET_NUM;i++)
    {
        if(m_hero.m_bullets[i].m_Free==false)
        {
            painter.drawPixmap(m_hero.m_bullets[i].m_X,m_hero.m_bullets[i].m_Y,m_hero.m_bullets[i].m_Bullet);
        }
    }




    //绘制爆炸
    for(int i=0;i<BOMB_NUM;i++)
    {
        if(m_bombs[i].m_Free==false)
        {
            painter.drawPixmap(m_bombs[i].m_X,m_bombs[i].m_Y,m_bombs[i].m_pixArr[m_bombs[i].m_index]);
        }
    }



    //测试子弹
    //painter.drawPixmap(temp_Bullet.m_X,temp_Bullet.m_Y,temp_Bullet.m_Bullet);

    //绘制敌机1
    for(int i=0;i<ENEMY1_NUM;i++)
    {
        if(m_enemy1s[i].m_Free==false)
        {
            painter.drawPixmap(m_enemy1s[i].m_X,m_enemy1s[i].m_Y,m_enemy1s[i].m_enemy1);
        }
    }

    //绘制敌机2
    for(int i=0;i<ENEMY2_NUM;i++)
    {
        if(m_enemy2s[i].m_Free2==false)
        {
            painter.drawPixmap(m_enemy2s[i].m_X2,m_enemy2s[i].m_Y2,m_enemy2s[i].m_enemy2);
        }
    }

    //绘制敌机3
    for(int i=0;i<ENEMY3_NUM;i++)
    {
        if(m_enemy3s[i].m_Free3==false)
        {
            painter.drawPixmap(m_enemy3s[i].m_X3,m_enemy3s[i].m_Y3,m_enemy3s[i].m_enemy3);
        }
    }
}

void MainScene::mouseMoveEvent(QMouseEvent *event)
{
    int x=event->x()-m_hero.m_Rect.width()*0.5;
    int y=event->y()-m_hero.m_Rect.height()*0.5;

    //边界检测
    if(x<=0)
        x=0;
    if(x>=GAME_WIDTH-m_hero.m_Rect.width())
        x=GAME_WIDTH-m_hero.m_Rect.width();
    if(y!=GAME_HEIGHT-m_hero.m_Rect.height())
        y=GAME_HEIGHT-m_hero.m_Rect.height();

    m_hero.setposition(x,y);
}

void MainScene::enemy1ToScene()
{
    m_recorder++;
    if(m_recorder<ENEMY1_INTERVAL)
    {
        return;
    }
    m_recorder=0;
    for(int i=0;i<ENEMY1_NUM;i++)
    {
        if(m_enemy1s[i].m_Free)
        {
            m_enemy1s[i].m_Free=false;

            //坐标
            m_enemy1s[i].m_X=rand()%(GAME_WIDTH-m_enemy1s[i].m_Rect.width()-10);
            m_enemy1s[i].m_Y=-m_enemy1s[i].m_Rect.height();
            break;
        }
    }
}

void MainScene::enemy2ToScene()
{
    m_recorder2++;
    if(m_recorder2<ENEMY2_INTERVAL)
    {
        return;
    }
    m_recorder2=0;
    for(int i=0;i<ENEMY2_NUM;i++)
    {
        if(m_enemy2s[i].m_Free2)
        {
            m_enemy2s[i].m_Free2=false;

            //坐标
            m_enemy2s[i].m_X2=rand()%(GAME_WIDTH-m_enemy2s[i].m_Rect2.width()-10);
            m_enemy2s[i].m_Y2=-m_enemy2s[i].m_Rect2.height();
            break;
        }
    }
}

void MainScene::enemy3ToScene()
{
    m_recorder3++;
    if(m_recorder3<ENEMY3_INTERVAL)
    {
        return;
    }
    m_recorder3=0;
    for(int i=0;i<ENEMY3_NUM;i++)
    {
        if(m_enemy3s[i].m_Free3)
        {
            m_enemy3s[i].m_Free3=false;

            //坐标
            m_enemy3s[i].m_X3=rand()%(GAME_WIDTH-m_enemy3s[i].m_Rect3.width()-10);
            m_enemy3s[i].m_Y3=-m_enemy3s[i].m_Rect3.height();
            break;
        }
    }
}

void MainScene::collisionDetection()
{
    //遍历所有非空闲的敌机
    for(int i=0;i<ENEMY1_NUM;i++)
    {
        //如果是空闲的，执行下一次
        if(m_enemy1s[i].m_Free)
        {
            continue;
        }

        //遍历所有非空闲的子弹
        for(int j=0;j<BULLET_NUM;j++)
        {
            //空闲子弹执行下一次
            if(m_hero.m_bullets[j].m_Free)
            {
                continue;
            }

            //如果子弹和飞机相交，发生碰撞，同时将飞机和子弹空闲状态设置为真
            if(m_enemy1s[i].m_Rect.intersects(m_hero.m_bullets[j].m_Rect))
            {
                m_enemy1s[i].m_Free=true;
                m_hero.m_bullets[j].m_Free=true;

                //播放爆炸效果
                for(int k=0;k<BOMB_NUM;k++)
                {
                    if(m_bombs[k].m_Free)
                    {
                        m_bombs[k].m_Free=false;
                        m_bombs[k].m_X=m_enemy1s[i].m_X+10;
                        m_bombs[k].m_Y=m_enemy1s[i].m_Y+30;
                        break;
                    }
                }
            }
        }
    }
}

void MainScene::collisionDetection2()
{
    //遍历所有非空闲的敌机
    for(int i=0;i<ENEMY2_NUM;i++)
    {
        //如果是空闲的，执行下一次
        if(m_enemy2s[i].m_Free2)
        {
            m_enemy2s[i].HP=2;
            continue;
        }

        //遍历所有非空闲的子弹
        for(int j=0;j<BULLET_NUM;j++)
        {
            //空闲子弹执行下一次
            if(m_hero.m_bullets[j].m_Free)
            {
                continue;
            }


            //如果子弹和飞机相交，发生碰撞，同时将飞机和子弹空闲状态设置为真
            if(m_enemy2s[i].m_Rect2.intersects(m_hero.m_bullets[j].m_Rect))
            {
                m_enemy2s[i].HP--;
                m_hero.m_bullets[j].m_Free=true;

                if(m_enemy2s[i].HP==0)
                {
                    m_enemy2s[i].m_Free2=true;
                    m_enemy2s[i].HP=2;
                    //播放爆炸效果
                    for(int k=0;k<BOMB_NUM;k++)
                    {
                    if(m_bombs[k].m_Free)
                    {
                        m_bombs[k].m_Free=false;
                        m_bombs[k].m_X=m_enemy2s[i].m_X2+10;
                        m_bombs[k].m_Y=m_enemy2s[i].m_Y2+30;
                        break;
                    }
                    }
                }
            }
        }
    }
}

void MainScene::collisionDetection3()
{
    //遍历所有非空闲的敌机
    for(int i=0;i<ENEMY3_NUM;i++)
    {
        //如果是空闲的，执行下一次
        if(m_enemy3s[i].m_Free3)
        {
            m_enemy3s[i].HP=2;
            continue;
        }

        //遍历所有非空闲的子弹
        for(int j=0;j<BULLET_NUM;j++)
        {
            //空闲子弹执行下一次
            if(m_hero.m_bullets[j].m_Free)
            {
                continue;
            }


            //如果子弹和飞机相交，发生碰撞，同时将飞机和子弹空闲状态设置为真
            if(m_enemy3s[i].m_Rect3.intersects(m_hero.m_bullets[j].m_Rect))
            {
                m_enemy3s[i].HP--;
                m_hero.m_bullets[j].m_Free=true;

                if(m_enemy3s[i].HP==0)
                {
                    m_enemy3s[i].m_Free3=true;
                    m_enemy3s[i].HP=2;
                    //播放爆炸效果
                    for(int k=0;k<BOMB_NUM;k++)
                    {
                    if(m_bombs[k].m_Free)
                    {
                        m_bombs[k].m_Free=false;
                        m_bombs[k].m_X=m_enemy3s[i].m_X3+10;
                        m_bombs[k].m_Y=m_enemy3s[i].m_Y3+30;
                        break;
                    }
                    }
                }
            }
        }
    }
}

